package com.baizhi.controller;


import com.baizhi.entity.User;
import com.baizhi.service.IUserService;
import com.baizhi.utils.JwtUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhangsan
 * @since 2022-06-27
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private IUserService us;
    @GetMapping("/selectAll")
    public List<User> selectAll(){
        return us.list();
    }
    @GetMapping("/login/{name}/{pwd}")
    public String login(@PathVariable("name") String name,@PathVariable("pwd") String pwd){
        QueryWrapper<User> wrapper=new QueryWrapper<>();
        wrapper.eq("name",name);
        wrapper.eq("pwd",pwd);
        User one = us.getOne(wrapper);
        if(one!=null){
            Map<String,String> map=new HashMap<>();
            map.put("userid",one.getId()+"");
            return JwtUtils.getToken(map);
        }else{
            return "登录失败";
        }
    }

    @GetMapping("/selectOne/{id}")
    public User selectOne(@PathVariable("id") int id){
        return us.getById(id);
    }
}

